package eddy.yang.wordpad;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

public class WordPadInternalFrame extends JInternalFrame {
	WordPadPanel pane;
	WordPadFrame worfrm;
	public WordPadInternalFrame(WordPadPanel pane, WordPadFrame worfrm) {
		super(pane.getTitle(), true, true, true, true);
		this.pane = pane;
		this.worfrm = worfrm;
		add(pane);
		
		addVetoableChangeListener(new VetoableChangeListener() {
		   public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
		      String name = event.getPropertyName();
		      Object value = event.getNewValue();
		      if(name.equals("closed") && value.equals(true)) {
		      	setSelected(true);
		      	int result = JOptionPane.showConfirmDialog(null, "Are you sure to close file! ", "", JOptionPane.YES_NO_OPTION);
		      	if(result == JOptionPane.YES_OPTION)
		      		doPerClose();
		      	else if(result != JOptionPane.YES_OPTION) {
		      		throw new PropertyVetoException("User canceles close", event);
		      	}
		      }
			}
		});
		
		setSize(600, 400);
	}
	
	public void doPerClose() {
		this.pane.doPerClose();
		this.worfrm.wordpaneVect.remove(pane);
	}
	
	public WordPadPanel getWordPadPanel() {
		return this.pane;
	}
}
